<template>
	<view>
		<view class="content">
			<mu-col span="12" lg="4" sm="6">
				<mu-date-input v-model="time" @change="getTime()" label="日记时间:" label-float full-width no-display>
				</mu-date-input>
			</mu-col>
			<mu-col span="12" lg="4" sm="6">
				<mu-select label="心情:" v-model="MyMood" full-width @change="getMood">
					<mu-option v-for="item,index in Mood" :key="item" :label="item" :value="item">
					</mu-option>
				</mu-select>
			</mu-col>
			<mu-col span="12" lg="4" sm="6">
				<mu-select label="天气:" v-model="WeatherInfo" full-width @change="getWeather">
					<mu-option v-for="item,index in Weather" :key="item" :label="item" :value="item">
					</mu-option>
				</mu-select>
			</mu-col>
			<view style="padding: 16rpx;">
				<mu-container>
					<mu-text-field @input="getText" v-model="DiaryFrom.diary" placeholder="今天发生了什么？" multi-line
						:rows="3" :rows-max="6"></mu-text-field>
					<text class="iconfont icon-huachujujuhuabaihua" style="padding-left: 10rpx; font-size: 30rpx;">

					</text>

					<br />
				</mu-container>
			</view>
			<view class="btn">
				<mu-button color="#1B9ADC" ripple round @click="storeDliaryInfo">
					保存日记
				</mu-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		name: "addDliary",
		computed: {
			...mapState(['Dliary'])
		},
		data() {
			return {
				Mood: [
					'开心', '难过', '平静', '幸福', '迷茫'
				],
				Weather: [
					'晴', '阴天', '多云'
				],
				MyMood: '',
				WeatherInfo: '',
				time: undefined,
				DiaryFrom: {
					diary: ''
				}
			};
		},
		methods: {
			// 获取时间
			getTime() {
				var date = new Date();
				var myyear = date.getFullYear();
				var mymonth = date.getMonth() + 1;
				var myweekday = date.getDate();
				if (mymonth < 10) {
					mymonth = "0" + mymonth;
				}
				if (myweekday < 10) {
					myweekday = "0" + myweekday;
				}
				return (myyear + "-" + mymonth + "-" + myweekday);
			},
			// 获取心情
			getMood() {
				console.log(this.MyMood);
			},
			// 获取天气
			getWeather() {
				console.log(this.WeatherInfo);
			},
			// 获取日记内容
			getText() {
				console.log(this.DiaryFrom.diary);
			},
			// 保存日记
			async storeDliaryInfo() {
				var date = new Date();
				var userInfo=uni.getStorageSync('userInfo');
				var DliaryInfo = {
					time: this.getTime(),
					// timeHM: date.getHours() + ' : ' + date.getMinutes(),
					mood: this.MyMood,
					weather: this.WeatherInfo,
					message: this.DiaryFrom.diary,
					userId:userInfo.id
				};
				await this.$api({url:"/add/diary",method:"POST"},DliaryInfo).then(res=>{
					this.$store.commit('setDliary2', DliaryInfo);
					console.log(res);
					uni.showToast({
						title:'保存成功',
						icon:'none'
					});
				})
				// 置空
				this.time='';
				this.MyMood='';
				this.WeatherInfo='';
				this.DiaryFrom.diary='';
			}
		}
	}
</script>

<style lang="less">
	.content {
		.btn {
			display: flex;
			justify-content: center;

		}
	}
</style>
